package br.usp.icmc.client.main;

import java.io.File;
import java.util.HashMap;
import java.util.Scanner;


import br.usp.icmc.client.controladores.ControllerClient;
import br.usp.icmc.client.controladores.ControllerInquiry;
import br.usp.icmc.client.controladores.ControllerLogin;
import br.usp.icmc.client.controladores.ControllerProvider;
import br.usp.icmc.client.entidades.Client;
import br.usp.icmc.client.entidades.Provider;
import br.usp.icmc.client.exception.RegisterException;
import br.usp.icmc.client.util.Login;
import br.usp.icmc.client.util.Util;

/***********************************************************
 * 
 * <b> <br> Projeto: Aplicativo Cliente <br><b>
 * 
 * Classe Console - Responsável pela interface com o usuário
 * 
 * @author felipe
 *
 ***********************************************************/


public class Console {
	ControllerLogin controllerLogin = new ControllerLogin();
	ControllerProvider controllerProvider = new ControllerProvider();
	ControllerClient controllerClinet = new ControllerClient();
	ControllerInquiry controllerInquiry = new ControllerInquiry();
	Login objLogin = new Login();
	Util util = new Util();
	Terminal terminal = new Terminal();
	private HashMap<String, Integer> hashCommands;
	private String argumento;
	private String terminalCommand;	
	
	
	public String getTerminalCommand() {
		return terminalCommand;
	}


	public void setTerminalCommand(String terminalCommand) {
		this.terminalCommand = terminalCommand;
	}


	public String getArgumento() {
		return argumento;
	}


	public void setArgumento(String argumento) {
		this.argumento = argumento;
	}

	/**
	 * Define os comandos do aplicativo
	 * 
	 */
	public Console() {
		hashCommands = new HashMap<String, Integer>();
		
		hashCommands.put("rss find-all", 2);
		hashCommands.put("rss find-name", 1);
		hashCommands.put("rss help", 3);
		hashCommands.put("rss install", 4);
		hashCommands.put("rss get-service", 5);
		hashCommands.put("rss show", 6);
		hashCommands.put("rss view", 7);
		hashCommands.put("rss exit", 8);
		hashCommands.put("rss create-project", 9);
		hashCommands.put("rss clear", 10);
		hashCommands.put("rss find-type", 11);
		hashCommands.put("default", 12);
		hashCommands.put("l", 13);
		hashCommands.put("r", 14);
		hashCommands.put("e", 15);
		hashCommands.put("rss load-project", 16);
		hashCommands.put("rss show-projects", 17);
		hashCommands.put("rss current-project", 18);
	}
	
	/**
	 * Realiza o tratamento das ações do aplicativo
	 * 
	 * @return
	 */
	public String menu(){
		String line;
		StringBuilder sb = new StringBuilder();
		Scanner scanner = new Scanner(System.in);
		System.out.print(">>");		
		line = scanner.nextLine();
		
		String[] aux = line.split(" ");
	    
		if(aux.length == 1){
			setTerminalCommand("default");
		} else if(hashCommands.containsKey(aux[0]+" "+aux[1])){
			if(aux.length > 1 && aux.length <= 2){
				setTerminalCommand(aux[0]+" "+aux[1]);
			} else if(aux.length > 2 && aux.length < 4) {
				setArgumento(aux[2]);
				setTerminalCommand(aux[0]+" "+aux[1]);
				
			}
		} else {
			setTerminalCommand("default");
		}
		
		
		return getTerminalCommand();
	}
	
	
	/**
	 * Menu da aplicação
	 * 
	 * @return
	 */
	public String menuLogin(){
		util.limparTela();		
		Scanner lerOpcao = new Scanner(System.in);
		//int opcao;	
		String comando;
		System.out.println("Welcome to Robotic Semantic Search Tool - RSSTool!");
		System.out.println("Type [L]ogin or [R]egister or [E]xit");
		System.out.print(">>");
		//System.out.print("Type your option: ");
		comando = lerOpcao.next();
		
		if(comando.equalsIgnoreCase("l") || comando.equalsIgnoreCase("r") || comando.equalsIgnoreCase("e")){
			setTerminalCommand(comando);
		} else {
			setTerminalCommand("default");
		}
		
		return getTerminalCommand().toLowerCase();
		
		/*
		if((opcao >= 0 && opcao <= 9)){
				return opcao;
		} 
		return -1;
		*/
	}
	
	
	/**
	 * Executa o menu do usuario logado 
	 * 
	 */
	public void executarMenuInicial(){
		String comando;
		comando = menu();	
			while(comando != null){			
				switch(hashCommands.get(comando)){
					case 1:
						terminal.findName(getArgumento());
						executarMenuInicial();
						break;
						
					case 2:
						terminal.findAll();
						executarMenuInicial();						
						break;
						
					case 3:
						terminal.help();
						executarMenuInicial();					
						break;
					case 4:
						System.out.println("Installing... ");
						terminal.buildPackage();
						break;
					case 5:
						terminal.getService(objLogin, getArgumento());
						executarMenuInicial();
						break;
					case 6:
						terminal.show();
						break;
						
					case 7:
						terminal.viewService(getArgumento());
						executarMenuInicial();
						break;
					case 8:
						terminal.exit();
						break;
					case 9:
						//terminal.createProject(getArgumento());
						terminal.createDirectory(objLogin, getArgumento());
						executarMenuInicial();
						break;
					case 10:
						terminal.clear();
						executarMenuInicial();
						break;
					case 11:
						terminal.findByWord(getArgumento());
						executarMenuInicial();
						break;
					case 12:
						System.out.println("Invalid Command!");
						executarMenuInicial();
						break;
					case 16:
						terminal.loadProject(getArgumento());
						executarMenuInicial();
					case 17:
						terminal.showProjects(objLogin);
						executarMenuInicial();
					case 18:
						terminal.currentProject();
						executarMenuInicial();
					default :
						System.out.println("Invalid Commnad!");
						executarMenuInicial();
						break;
						
				}
				comando = menu();
			}
			//util.limparTela();
		
		
	}	
	
	

	
	
	/**
	 * Executa a aplicação
	 * 
	 * 
	 */
	public void executar(){	
		
		String opcao;
		Scanner scanner = new Scanner(System.in);
		opcao = menuLogin();
			
		while(opcao != null){
			switch(hashCommands.get(opcao)){
				case 12:
					System.out.println("Invalid Command!");
					executar();
				break;
				case 13:
					util.limparTela();
					controllerLogin.loadLogin();
					objLogin = controllerLogin.getLogin();
					util.limparTela();
					//terminal.setProjectName(objLogin.getPath());
					System.out.println("To discover all available commands type rss help");
					executarMenuInicial();
					break;
				case 14:
					//util.limparTela();
					System.out.println("Welcome to the registration area!");
					System.out.println("Enter your full name: ");
					String fullName = scanner.nextLine();
					System.out.println("Enter your username: ");
					String userName = scanner.nextLine();
					System.out.println("Enter your password: ");
					String pass = scanner.nextLine();
					System.out.println("Please, repeat your password: ");
					String rpass = scanner.nextLine();
					while(!pass.equals(rpass)){
						System.out.println("Please, you must type the same password.");
						System.out.println("Repeat your password: ");
						rpass = scanner.nextLine();
					}
					System.out.println("Enter your email: ");
					String email = scanner.nextLine();				
					
					try {
						controllerClinet.addClient(email, userName, pass, fullName);
					} catch (RegisterException e) {
						System.out.println("Error when adding client! Try Again!!");
					}				
					
					break;
				case 15:
						terminal.exit();
						break;
				
				default :
					System.out.println("Invalid Option!");
					System.out.println();
					executar();
					break;
			}
			opcao = menuLogin();
		}
		//util.limparTela();
	}
	

}
